package com.hyy.taobao.dao.impl;

import com.hyy.taobao.api.entity.MyCart;
import com.hyy.taobao.dao.IMyCartDao;

import java.sql.*;
import java.util.HashSet;
import java.util.ResourceBundle;
import java.util.Set;

public class MyCartDaoImpl implements IMyCartDao {
    @Override
    public boolean myCartInsertGoods(MyCart myCart) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        ResourceBundle resourceBundle = ResourceBundle.getBundle("JDBC");
        String driver = resourceBundle.getString("driver");
        String url = resourceBundle.getString("url");
        String user1 = resourceBundle.getString("user");
        String pwd = resourceBundle.getString("password");
        String checkMyCartSql = resourceBundle.getString("checkMyCartSql");
        String myCartInsertSql = resourceBundle.getString("myCartInsertSql");
        String myCartInsertUpdateSql = resourceBundle.getString("myCartInsert_updateSql");

        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, user1, pwd);
            preparedStatement = connection.prepareStatement(checkMyCartSql);
            preparedStatement.setString(1, myCart.getUserName());
            preparedStatement.setString(2, myCart.getGoodsName());
            resultSet = preparedStatement.executeQuery();
            if (!resultSet.next()) {
                preparedStatement = connection.prepareStatement(myCartInsertSql);
                preparedStatement.setString(1, myCart.getUserName());
                preparedStatement.setString(2, myCart.getGoodsName());
                preparedStatement.setInt(3, myCart.getGoodsPrice());
                preparedStatement.setInt(4, myCart.getGoodsNumber());
                preparedStatement.executeUpdate();
            } else {
                int goodsNumber = resultSet.getInt("goodsNumber");
                int newNumber = myCart.getGoodsNumber() + goodsNumber;
                preparedStatement = connection.prepareStatement(myCartInsertUpdateSql);
                preparedStatement.setInt(1, newNumber);
                preparedStatement.setString(2, myCart.getUserName());
                preparedStatement.setString(3, myCart.getGoodsName());
                preparedStatement.executeUpdate();
            }
        } catch (Exception e) {
            System.out.println("添加失败咯！");
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException e) {
                System.out.println("ResultSet关闭失败！");
                e.printStackTrace();
                return false;
            }
            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
            } catch (SQLException e) {
                System.out.println("PreparedStatement关闭失败！");
                e.printStackTrace();
                return false;
            }
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                System.out.println("Connection关闭失败！");
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    @Override
    public Set<MyCart> myCartSelectGoods(MyCart myCart) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Set<MyCart> myCartSet = new HashSet<>();
        ResourceBundle resourceBundle = ResourceBundle.getBundle("JDBC");
        String driver = resourceBundle.getString("driver");
        String url = resourceBundle.getString("url");
        String user1 = resourceBundle.getString("user");
        String pwd = resourceBundle.getString("password");
        String myCartSelectGoodsSql = resourceBundle.getString("myCartSelectGoodsSql");
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, user1, pwd);
            preparedStatement = connection.prepareStatement(myCartSelectGoodsSql);
            preparedStatement.setString(1, myCart.getUserName());
            resultSet = preparedStatement.executeQuery();
            if (resultSet!=null) {
                while (resultSet.next()) {
                    String goodsName = resultSet.getString("goodsName");
                    int goodsPrice = resultSet.getInt("goodsPrice");
                    int goodsNumber = resultSet.getInt("goodsNumber");
                    MyCart myCart1 = new MyCart(goodsName, goodsPrice, goodsNumber);
                    myCartSet.add(myCart1);
                }
            } else {
                return null;
            }
        } catch (Exception e) {
            System.out.println("查看失败！");
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException e) {
                System.out.println("ResultSet关闭失败！");
                e.printStackTrace();
                return null;
            }
            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
            } catch (SQLException e) {
                System.out.println("PreparedStatement关闭失败！");
                e.printStackTrace();
                return null;
            }
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                System.out.println("Connection关闭失败！");
                e.printStackTrace();
                return null;
            }
        }
        return myCartSet;
    }

    @Override
    public boolean myCartDeleteGoods(MyCart myCart, int judgement) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        ResourceBundle resourceBundle = ResourceBundle.getBundle("JDBC");
        String driver = resourceBundle.getString("driver");
        String url = resourceBundle.getString("url");
        String user1 = resourceBundle.getString("user");
        String pwd = resourceBundle.getString("password");
        String checkMyCartSql = resourceBundle.getString("checkMyCartSql");
        String myCartDeleteGoodsSql = resourceBundle.getString("myCartDeleteGoodsSql");
        String myCartDeleteGoodsUpdateSql = resourceBundle.getString("myCartDeleteGoodsUpdateSql");
        String myCartDeleteAllGoodsSql = resourceBundle.getString("myCartDeleteAllGoodsSql");
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, user1, pwd);
            if (judgement == 10) {
                preparedStatement = connection.prepareStatement(myCartDeleteAllGoodsSql);
                preparedStatement.setString(1, myCart.getUserName());
                preparedStatement.executeUpdate();
            } else {
                preparedStatement = connection.prepareStatement(checkMyCartSql);
                preparedStatement.setString(1, myCart.getUserName());
                preparedStatement.setString(2, myCart.getGoodsName());
                resultSet = preparedStatement.executeQuery();
                if (judgement == 0 && resultSet!=null) {
                    resultSet.next();
                    preparedStatement = connection.prepareStatement(myCartDeleteGoodsSql);
                    preparedStatement.setString(1, myCart.getUserName());
                    preparedStatement.setString(2, myCart.getGoodsName());
                    preparedStatement.executeUpdate();
                } else if ((judgement ==1 ||judgement == -1 )&& resultSet!=null) {
                    resultSet.next();
                    int goodsNumber = resultSet.getInt("goodsNumber");
                    int newGoodsNumber = judgement + goodsNumber;
                    preparedStatement = connection.prepareStatement(myCartDeleteGoodsUpdateSql);
                    preparedStatement.setInt(1, newGoodsNumber);
                    preparedStatement.setString(2, myCart.getUserName());
                    preparedStatement.setString(3, myCart.getGoodsName());
                    preparedStatement.executeUpdate();
                    if (judgement==-1 &&goodsNumber==1){
                        preparedStatement = connection.prepareStatement(myCartDeleteGoodsSql);
                        preparedStatement.setString(1, myCart.getUserName());
                        preparedStatement.setString(2, myCart.getGoodsName());
                        preparedStatement.executeUpdate();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("删减商品失败咯！");
            return false;
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException e) {
                System.out.println("ResultSet关闭失败！");
                e.printStackTrace();
                return false;
            }
            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
            } catch (SQLException e) {
                System.out.println("PreparedStatement关闭失败！");
                e.printStackTrace();
                return false;
            }
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                System.out.println("Connection关闭失败！");
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }
}
